//index.js
//获取应用实例
const app = getApp()
Component({
  data: {
    StatusBar: app.globalData.StatusBar,
    Custom: app.globalData.Custom,
    CustomBar: app.globalData.CustomBar,
    work:null,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [
      {
        title: '隐患上报',
        img: 'https://image.weilanwl.com/color2.0/plugin/wdh2236.jpg',
        url: '../trouble/trouble',
        type: ''
      },
      {
        title: '隐患确认',
        img: 'https://image.weilanwl.com/color2.0/plugin/qpct2148.jpg',
        url: '../confTrou/confTrou',
        type:'总监代表'
      },
      {
        title: '上传设备吊装条件验收照片',
        img: 'https://image.weilanwl.com/color2.0/plugin/qpczdh2307.jpg',
        url: '../hoist/hoist',
        type: ''
      }
    ]
  },
  methods: {
    onLoad() {
      let that = this;
      let _custom = wx.getMenuButtonBoundingClientRect()
      setTimeout(() => {
        if (that.data.CustomBar === 'undefined' || typeof (that.data.CustomBar === 'undefined')) {
          let new_custom = _custom
          that.setData({
            CustomBar: new_custom.bottom + new_custom.top - that.data.StatusBar
          })
        }
      }, 800);
      
      // 获取用户信息
      /*wx.getSetting({
        success: res => {
          if (!res.authSetting['scope.userInfo']) {
            console.log("index getsetting fail 授权失效")
           
            wx.redirectTo({
              url: '/pages/auth/auth'
            })
          }else{
            console.log("index getSetting set suc")
            if (this.data.canIUse) {
              app.userInfoReadyCallback = res => {
                this.setData({
                  userInfo: res.userInfo,
                  hasUserInfo: true
                })
              }
            }else{
              wx.getUserInfo({
                success: res => {
                  app.globalData.userInfo = res.userInfo
                  this.setData({
                    userInfo: res.userInfo,
                    hasUserInfo: true
                  })
                }
              })
            }
            //this.validate(); //感觉不应该放在onload里面，凡是前后端交互的地方都要验证一下
          }
        }
      })*/
      //模块显示与权限关联
      //可能回调可能不回调，所以if  else
      if (app.globalData.userWork && app.globalData.userWork!=''){
        this.setData({ work: app.globalData.userWork})
        console.log("work", that.data.work);
      }
      else
      app.userWorkCallback = userWork => {  //比app中优先执行才会回调
        if(userWork!=''){
          this.setData({ work: userWork })
          console.log("callback work", that.data.work);
        }
      }
      //回调判断的userWork至此结束
    },
    //这个页面应该用不到了
    validate: function () { //判断用户认证是否存在
      let openid= wx.getStorageSync('open');
      if (!openid) {//高级一点就判断token是否过期 过期清缓存  app.globalData.userName
        console.log("index validate no openid");
        wx.showToast({
          title: '认证过期',
          icon: 'loading',
          duration: 1500
        })
        wx.navigateTo({
          url: '/pages/auth/auth',
        });
      }      
    },
    showModal(e) {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    },
    hideModal(e) {
      this.setData({
        modalName: null
      })
    },
    onShareAppMessage() {
      return {
        title: '苏州地铁智能监管系统',
        imageUrl: 'https://image.weilanwl.com/color2.0/share2215.jpg',
        path: '/pages/index/index'
      }
    },
    toChild(e) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      })
    },
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function' &&
        this.getTabBar()) {
        this.getTabBar().setData({
          selected: 0
        })
      }
    }
  },
  
});
/*Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    motto: 'Hi 开发者！',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  
})*/
